# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=gnatcoll-db
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=25.0.0
pkgrel=2
pkgdesc="GNAT Components Collection - Database packages (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url="https://github.com/AdaCore/gnatcoll-db"
msys2_references=(
  'aur: gnatcoll-db2ada'
  'gentoo: dev-ada/gnatcoll-db'
)
license=('spdx:GPL-3.0-or-later WITH GCC-exception-3.1')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-ada"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gnatcoll-core"
         "${MINGW_PACKAGE_PREFIX}-gnatcoll-iconv"
         "${MINGW_PACKAGE_PREFIX}-libgpr"
         "${MINGW_PACKAGE_PREFIX}-postgresql"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-xmlada")
makedepends=("${MINGW_PACKAGE_PREFIX}-gprbuild")
source=("https://github.com/AdaCore/gnatcoll-db/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('167eedebb6347e69a674f5747bcdcbb050fbf06c0fa2c069efcad32dc10308f0')

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  cp -r "${srcdir}/${_realname}-${pkgver}" "${srcdir}"/build-${MSYSTEM}
  cd "${srcdir}"/build-${MSYSTEM}

  declare -a GPRBUILD_FLAGS=(
    "-j$(nproc)"
    "-XBUILD=PROD"
    "-XLIBRARY_TYPE=relocatable"
    "-XGNATCOLL_VERSION=${pkgver}"
    "-XXMLADA_BUILD=relocatable"
    "-XGPR_BUILD=relocatable"
    "-XGNATCOLL_SQLITE=external")

  export GPR_PROJECT_PATH="${srcdir}"/build-${MSYSTEM}/sql:"${srcdir}"/build-${MSYSTEM}/sqlite:"${srcdir}"/build-${MSYSTEM}/xref

  for _lib in sql sqlite xref postgres; do
    gprbuild -p -P ${_lib}/gnatcoll_${_lib}.gpr "${GPRBUILD_FLAGS[@]}"
  done

  for _tool in gnatinspect gnatcoll_db2ada; do
    gprbuild -p -P ${_tool}/${_tool}.gpr "${GPRBUILD_FLAGS[@]}"
  done
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  GPRINSTALL_FLAGS=(
    "--prefix=${pkgdir}${MINGW_PREFIX}"
    "-XBUILD=PROD"
    "-XLIBRARY_TYPE=relocatable"
    "-XGNATCOLL_VERSION=${pkgver}"
    "-XXMLADA_BUILD=relocatable"
    "-XGPR_BUILD=relocatable"
    "--build-var=LIBRARY_TYPE"
    "--build-name=relocatable"
  )

  for _lib in sql sqlite xref postgres; do
    GPR_PROJECT_PATH="${pkgdir}${MINGW_PREFIX}/share/gpr" \
    gprinstall -f -p -P ${_lib}/gnatcoll_${_lib}.gpr "${GPRINSTALL_FLAGS[@]}"
  done

  for _tool in gnatinspect gnatcoll_db2ada; do
    GPR_PROJECT_PATH="${pkgdir}${MINGW_PREFIX}/share/gpr" \
    gprinstall -f -p -P ${_tool}/${_tool}.gpr "${GPRINSTALL_FLAGS[@]}"
  done

  # Copy License Files
  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}" \
    "${srcdir}/gnatcoll-db-${pkgver}"/COPYING*
}
